//
//  FindCar_CarDetails_TableViewController.m
//  CarEncyclopedia
//
//  Created by lanou3g on 15/7/21.
//  Copyright (c) 2015年 ThighXingClub. All rights reserved.
//

#import "FindCar_CarDetails_TableViewController.h"
#import "FindCar_CarDetails_FirstSection_TableViewCell.h"
#import "FindCar_CarDetails_FirstSection_SecondRow_TableViewCell.h"
#import "FindCar_CarDetails_SecondSection_TableViewCell.h"
#import "ImageDownLoader.h"
#define url @"http://baojia.qichecdn.com/priceapi3.9.2/services/specsummary/get?cityid=0&myLat=0&myLon=0&order=0&pageindex=1&pagesize=20&provinceid=0&specid="
@interface FindCar_CarDetails_TableViewController ()<UIAlertViewDelegate>


@property(nonatomic,retain) NSDictionary *ResultDic;
@property(nonatomic,retain) NSArray *DealerlistArray;
@property(nonatomic,retain) NSString *tempPhone;
@property(nonatomic,retain) FindCar_CarDetails_SecondSection_TableViewCell *cell;

@property(nonatomic,retain) NSString *PhoneNum;

@end

@implementation FindCar_CarDetails_TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.view.backgroundColor=[UIColor colorWithRed:255/255.0 green:215/255.0 blue:0/255.0 alpha:1];
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_CarDetails_FirstSection_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell7"];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_CarDetails_FirstSection_SecondRow_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell8"];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FindCar_CarDetails_SecondSection_TableViewCell" bundle:nil] forCellReuseIdentifier:@"cell9"];
    
    
    NSLog(@"%@",self.specidStr);
    
    NSMutableString *PushToStr=[NSMutableString stringWithFormat:url,nil];
    
    NSString *ID=[NSString stringWithFormat:@"%@",self.specidStr];
    
    [PushToStr appendFormat:ID,nil];
    
    NSLog(@"==========%@",PushToStr);
    
    
    NSURL *Url=[NSURL URLWithString:PushToStr];
    
    NSURLRequest *request=[NSURLRequest requestWithURL:Url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            
            self.ResultDic=[dic objectForKey:@"result"];
            
            
            self.DealerlistArray=[self.ResultDic objectForKey:@"dealerlist"];
            
            
//            NSLog(@"%@",self.ResultDic);
            
            [self.tableView reloadData];
            
            
        }
    }];
    
    
}

//设置区头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0;
        
        
    }
    
    return 10;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return self.DealerlistArray.count +1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        
        return 2;
        
    }
    
    
    // Return the number of rows in the section.
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        FindCar_CarDetails_FirstSection_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell7"];
        
        ImageDownLoader *imageDown=[ImageDownLoader new];
        
        [imageDown requestImageUrl:[self.ResultDic objectForKey:@"logo"] successBlock:^(NSData *Data) {
            cell.ShowTitleImage.image=[UIImage imageWithData:Data];
            
        } errorBlock:^(NSError *Error) {
            
        }];
        
        return cell;
        
    }
    
    if (indexPath.section==0 &&indexPath.row ==1) {
    FindCar_CarDetails_FirstSection_SecondRow_TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell8"];
        
        
        cell.CarNameLabel.text=self.Car_name_Str;
        
        cell.CarStyleNameLabel.text=self.Car_Style_nameStr;
        
        
        NSString *Str1=@"万起";
        NSString *Str2=@"万";
        
        
        NSMutableString *PriceString=[NSMutableString stringWithFormat:@"%@",[self.ResultDic objectForKey:@"minprice"]];
        [PriceString appendFormat:Str1,nil];
        
        
        cell.LostPriceLabel.text=PriceString;
        
        
        NSMutableString *PriceString1=[NSMutableString stringWithFormat:@"%@",[self.ResultDic objectForKey:@"facprice"]];
        [PriceString1 appendFormat:Str2,nil];
        
        
        cell.GuidePriceLabel.text=PriceString1;
        
        [cell.ReceiveFunctionButton addTarget:self action:@selector(ReceiveFunctionButton:) forControlEvents:UIControlEventTouchUpInside];
        
        
        return cell;
    }

    
    self.cell=[tableView dequeueReusableCellWithIdentifier:@"cell9"];
    
    self.cell.DealershortNameLabel.text=[[self.DealerlistArray objectAtIndex:indexPath.section -1] objectForKey:@"dealershortname"];
    self.cell.DealerAddressLabel.text=[[self.DealerlistArray objectAtIndex:indexPath.section -1] objectForKey:@"dealeraddress"];
    
    NSString *temp=@"万";
    
    NSMutableString *tempStr=[NSMutableString stringWithFormat:@"%@",[[self.DealerlistArray objectAtIndex:indexPath.section -1] objectForKey:@"dealerprice"]];
    
    
    
    [tempStr appendFormat:temp,nil];
   
    self.cell.DealerPriceLabel.text=tempStr;
    
    self.cell.OrderrangeLabel.text=[[self.DealerlistArray objectAtIndex:indexPath.section -1] objectForKey:@"orderrange"];
    
    
    
    
    [self.cell.PhoneNumButton addTarget:self action:@selector(PhoneNumButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.cell.PhoneNumLabel.text=[[self.DealerlistArray objectAtIndex:indexPath.section-1 ] objectForKey:@"dealerphone"];
    self.cell.PhoneNumButton.tag =indexPath.section;
    
    self.cell.PhoneNumLabel.tag=indexPath.section;
    
    
    /*
//    self.tempPhone =[[self.DealerlistArray objectAtIndex:indexPath.section -1 ] objectForKey:@"dealerphone"];
//    
//    
//    [cell.PhoneNumButton setTitle:self.tempPhone forState:UIControlStateNormal];
    
    
//    
//        NSDictionary *Dic=[self.DealerlistArray objectAtIndex:indexPath.section-1];
//    
//     NSString *PHONE=[Dic objectForKey:@"dealerphone"];
//    
//     NSLog(@"%@",PHONE);
   
    
    
    
    
    
 
    
//    NSLog(@"🇮🇹%@",self.tempPhone);
    */
    
    // Configure the cell...
    
    return self.cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 0 && indexPath .row ==0) {
        return 200;
    }
    
    
    if (indexPath.section == 0 && indexPath .row ==1) {
        return 70;
    }
    
    return 150;
    
    
    
}

//收藏功能
-(void)ReceiveFunctionButton:(UIButton *)button{
    
    //后期添加数据库功能
    
    
    UIAlertView *View=[[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功咯~" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [View show];
    
}

-(void)PhoneNumButton:(UIButton *)button {
    
    
    NSMutableArray *TempArray=[NSMutableArray array];
    for (int i =0; i<self.DealerlistArray.count; i++) {
        NSDictionary *dic=[self.DealerlistArray objectAtIndex:i];
        NSString *tempStr=[dic objectForKey:@"dealerphone"];
        [TempArray addObject:tempStr];
        
    }
    

    
    for (int i =0; i <TempArray.count ; i++) {
        if (button.tag == i+1) {
           self.PhoneNum= [[self.DealerlistArray objectAtIndex:i] objectForKey:@"dealerphone"];
            
        }
    }
    
    
    NSString *tempStr=@"请拨打";
    NSMutableString *Str=[NSMutableString stringWithFormat:@"%@",self.PhoneNum];
    [Str insertString:tempStr atIndex:0];
    
    
    
    
    UIAlertView *View=[[UIAlertView alloc] initWithTitle:@"电话" message:Str delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"呼叫", nil];
    
    View.delegate=self;
    
    [View show];
}
//抓取UIAlertView上的button点击方法
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    
    NSString *phone=@"tel://";
    NSMutableString *phone1=[NSMutableString stringWithFormat:phone,nil];
    NSString *phone2=self.PhoneNum;
    [phone1 appendFormat:phone2,nil];
    
    
    
    if (buttonIndex == 1) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phone1]];
        
        
        
        
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
